#include "library.h"

LIST_HEAD(user_list);
User *current_user = NULL;

// 管理员登录
void admin_login()
{
    char id[20];
    char pwd[20];

    printf("请输入ID:");
    fgets(id, sizeof(id), stdin);
    id[strcspn(id, "\n")] = '\0';

    printf("请输入密码:");
    fgets(pwd, sizeof(pwd), stdin);
    pwd[strcspn(pwd, "\n")] = '\0';

    User *u;
    int found = 0;
    list_for_each_entry(u, &user_list, list)
    {
        if (u->type == 0 &&
            strcmp(u->id, id) == 0 &&
            strcmp(u->pwd, pwd) == 0)
        {
            found = 1;
            current_user = u;
            break;
        }
    }
    if (found)
    {
        printf("管理员登录成功\n");
        admin_menu();
    }
    else
    {
        printf("用户名或密码错误\n");
    }
}

// 读者登录
void reader_login()
{
    char id[20];
    char pwd[20];

    printf("请输入ID:");
    fgets(id, sizeof(id), stdin);
    id[strcspn(id, "\n")] = '\0';

    printf("请输入密码:");
    fgets(pwd, sizeof(pwd), stdin);
    pwd[strcspn(pwd, "\n")] = '\0';

    User *u;
    int found = 0;
    list_for_each_entry(u, &user_list, list)
    {
        if (u->type == 1 &&
            strcmp(u->id, id)==0 &&
            strcmp(u->pwd, pwd)==0)
        {
            found = 1;
            current_user = u;
            break;
        }
    }
    if (found)
    {
        printf("管理员登录成功\n");
        reader_menu();
    }
    else
    {
        printf("用户名或密码错误");
    }
}

// 管理员注册
void admin_register()
{
    User *new_admin = malloc(sizeof(User));
    if (!new_admin)
    {
        perror("内存分配错误");
        return;
    }

    printf("\n--- 管理员注册 ---\n");

    // 获取ID
    printf("ID: ");
    fgets(new_admin->id, sizeof(new_admin->id), stdin);
    new_admin->id[strcspn(new_admin->id, "\n")] = '\0';

    // 检查ID唯一性
    User *u;
    int id_exists = 0;
    list_for_each_entry(u, &user_list, list)
    {
        if (strcmp(u->id, new_admin->id) == 0)
        {
            id_exists = 1;
            break;
        }
    }
    if (id_exists)
    {
        printf("该ID已存在\n");
        free(new_admin);
        return;
    }

    // 获取其他信息
    printf("姓名: ");
    fgets(new_admin->name, sizeof(new_admin->name), stdin);
    new_admin->name[strcspn(new_admin->name, "\n")] = '\0';

    printf("密码: ");
    fgets(new_admin->pwd, sizeof(new_admin->pwd), stdin);
    new_admin->pwd[strcspn(new_admin->pwd, "\n")] = '\0';

    new_admin->type = ADMIN;
    INIT_LIST_HEAD(&new_admin->borrowed_books);
    INIT_LIST_HEAD(&new_admin->list);
    list_add_tail(&new_admin->list, &user_list);
    printf("管理员注册成功\n");
}

// 读者注册
void reader_register()
{
    User *new_reader = malloc(sizeof(User));
    if (!new_reader)
    {
        perror("内存分配错误");
        return;
    }

    printf("输入ID:");
    fgets(new_reader->id, sizeof(new_reader->id), stdin);
    new_reader->id[strcspn(new_reader->id, "\n")] = '\0';

    User *u;
    list_for_each_entry(u, &user_list, list)
    {
        if (strcmp(u->id, new_reader->id) == 0)
        {
            printf("此id已存在");
            free(new_reader);
            return;
        }
    }

    printf("输入名字:");
    fgets(new_reader->name, sizeof(new_reader->name), stdin);
    new_reader->name[strcspn(new_reader->name, "\n")] = '\0';

    printf("输入密码:");
    fgets(new_reader->pwd, sizeof(new_reader->pwd), stdin);
    new_reader->pwd[strcspn(new_reader->pwd, "\n")] = '\0';

    new_reader->type == 1;
    INIT_LIST_HEAD(&new_reader->borrowed_books);

    list_add_tail(&new_reader->list, &user_list);
    printf("读者创建成功\n");
}

// 访客浏览
void guest_browse()
{
    printf("\n---访客浏览模式---\n");
    show_all_books();

    printf("\n1. 返回主菜单\n");
    printf("请选择操作: ");
    int choice;
    scanf("%d", &choice);
    while (getchar() != '\n')
        ;
}

// 显示所有用户
void show_all_users()
{
    printf("\n用户列表\n");
    printf("%-10s %-10s %-15s %-10s\n", "用户类型", "用户ID", "姓名", "密码");

    User *u;
    list_for_each_entry(u, &user_list, list)
    {
        printf("%-10s %10s %15s %10s\n", u->type == 0 ? "管理员" : "读者", u->id, u->name, u->pwd);
    }
}

// 删除用户
void delete_user()
{
    show_all_users();

    char target_id[20];
    printf("输入要删除的读者ID:");
    fgets(target_id, sizeof(target_id), stdin);
    target_id[strcspn(target_id, "\n")] = '\0';

    User *u, *tmp;
    int found = 0;

    list_for_each_entry_safe(u, tmp, &user_list, list)
    {
        if (strcmp(u->id, target_id) == 0)
        {
            found = 1;

            // 检查是否当前用户
            if (strcmp(current_user->id ,target_id)==0)
            {
                printf("无法删除正在登录的用户");
                return;
            }

            // 检查有无没归还的图书
            if (!list_empty(&u->borrowed_books))
            {
                printf("有未归还的图书，无法删除");
                return;
            }
            if (u)
            {
                // 清空缓存
                Book *book, *tmp;
                list_for_each_entry_safe(book, tmp, &book_list, list)
                {
                    list_del(&book->list);
                    free(book);
                }

                // 删除
                list_del(&u->list);
                free(u);
            }
        }
        if (!found)
        {
            printf("未找到该用户");
        }
    }
}
